﻿using System.IO;
using WJP.Serialization;

namespace System
{
    /// <summary>object 类型的帮助方法。</summary>
    public static class ObjectHelper
    {
        /// <summary>使用序列化器来序列化反序列化的方式，深度复制一个对象</summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T Clone<T>(T obj)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                try
                {
                    ISerializationFormatter formatter = SerializationFormatterFactory.GetFormatter();
                    formatter.Serialize(memoryStream, obj);
                    memoryStream.Position = 0L;
                    return (T)formatter.Deserialize(memoryStream);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

    }
}